/**
 * Created by raniys on 4/27/17.
 */

var mainApp = angular.module('mainApp', [])
    .filter('trustAsResourceUrl', ['$sce', function ($sce) {
        return function (val) {
            return $sce.trustAsResourceUrl(val);
        };
    }]);

//Controller间通信的服务
mainApp.factory('Service', function () {
    return {
        //网络状态
        networkReady: false
    }
});

mainApp.controller('mainController', function ($scope, Service, $http) {
    $scope.service = Service;

    $scope.onRegisterClick = function () {
        const url = "/auth/register";
        $scope.httpPost(url, {
            username: 'admin',
            password: '123456'
        }, function (data) {
            console.log(data)
        });
    };

    $scope.onLoginClick = function () {
        const url = "/auth/login";
        $scope.httpPost(url, {
            username: 'testsdd',
            password: 'wvew435345'
        }, function (data) {
            console.log(data)
        });
    };

    /** http request **/
    $scope.httpPost = function (url, data, callback) {
        $http.post(url, data, {
            timeout: 5 * 1000
        }).then(function successCallback(response) {
            if (response.status === 200) {
                callback(response.data)
            } else {
                console.error("POST " + url + " error: " + response.status + " description: " + response.statusText);
            }
        }, function errorCallback(error) {
            if (error.status === -1) {
                console.error("POST " + url + " 请求超时");
            } else {
                console.error("POST " + url + " 服务器网络连接错误");
            }
        });
    };

    $scope.httpGet = function (url, data, callback) {
        $http.get(url, {
            timeout: 5 * 1000,
            params: data
        }).then(function successCallback(response) {
            if (response.status === 200) {
                callback(response.data)
            } else {
                console.error("GET " + url + " error: " + response.status + " description: " + response.statusText);
            }
        }, function errorCallback(error) {
            if (error.status === -1) {
                console.error("GET " + url + " 请求超时");
            } else {
                console.error("GET " + url + " 服务器网络连接错误");
            }
        });
    };

    $scope.httpDelete = function (url, data, callback) {
        $http.delete(url, {
            timeout: 5 * 1000,
            data: data
        }).then(function successCallback(response) {
            if (response.status === 200) {
                callback(response.data)
            } else {
                console.error("DELETE " + url + " error: " + response.status + " description: " + response.statusText);
            }
        }, function errorCallback(error) {
            if (error.status === -1) {
                console.error("DELETE " + url + " 请求超时");
            } else {
                console.error("DELETE " + url + " 服务器网络连接错误");
            }
        });
    };
});